package com.myk.game.gobangchess.ai.shock;

public class Kill_judging_deep4 {

    public static int[] kill_judging_deep4(int[][] board, int playerTurn) {
        int[] result = {0, -1, -1 , -1};
        int playerNum = (playerTurn == 1) ? 0 : 1;
        int[] result1 = Kill_judging_deep3.kill_judging_deep3(board,playerTurn);
        int isKillDeep3 = result1[0];

        if (isKillDeep3 != 0) {
            result[0] = 1;
            result[1] = result1[1];
            result[2] = result1[2];
            result[3] = result1[3];
        } else {
            for (int x = 0; x < 8; x++) {
                for (int y = 0; y < 8; y++) {
                    if (board[y][x] == 0) {
                        int[][] boardNew1 = new int[8][8];
                        copyBoard(board, boardNew1);

                        boardNew1[y][x] = playerTurn;
                        boardNew1 = Pointsetting.pointsetting(boardNew1, x, y);

                        int[] isKill = Kill_judging_deep1.kill_judging_deep1(boardNew1);

                        if (isKill[1 - playerNum] != 1 && isKill[playerNum] == 1) {
                            int stillKilling = 1;
                            int[] possibleSolveRange = Find_possible_solve.find_possible_solve(board, playerTurn);

                            for (int x2 = possibleSolveRange[2]; x2 <= possibleSolveRange[3] && stillKilling == 1; x2++) {
                                for (int y2 = possibleSolveRange[0]; y2 <= possibleSolveRange[1] && stillKilling == 1; y2++) {
                                    if (boardNew1[y2][x2] == 0 ) {
                                        int[][] boardNew2 = new int[8][8];
                                        copyBoard(boardNew1, boardNew2);

                                        boardNew2[y2][x2] = -playerTurn;
                                        boardNew2 = Pointsetting.pointsetting(boardNew2, x2, y2);

                                        int[] result2 = Kill_judging_deep3.kill_judging_deep3(boardNew2,playerTurn);
                                        stillKilling *= result2[0];
                                    }
                                }
                            }

                            if (stillKilling == 1) {
                                result[0] = 1;
                                result[1] = x + 1;
                                result[2] = y + 1;
                                result[3] = 4;
                                return result;
                            }
                        }
                    }
                }
            }
        }

        return result;
    }

    private static void copyBoard(int[][] source, int[][] destination) {
        for (int i = 0; i < source.length; i++) {
            System.arraycopy(source[i], 0, destination[i], 0, source[i].length);
        }
    }
}
